/**
 * $Revision$
 * $Date$
 *
 * This file is part of the Solveur repository software.
 * Copyright (C) 2012 Solveur developer team
 * See http://code.google.com/p/solveur/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/

package gougeon.android.solveur;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import android.util.Log;

public final class Mots {

	private static final Map<String, List<String>> MotsDe2Lettres = new HashMap<String, List<String>>();
	private static final Map<String, List<String>> MotsDe3Lettres = new HashMap<String, List<String>>();
	private static final Map<String, List<String>> MotsDe4Lettres = new HashMap<String, List<String>>();
	private static final Map<String, List<String>> MotsDe5Lettres = new HashMap<String, List<String>>();
	private static final Map<String, List<String>> MotsDe6Lettres = new HashMap<String, List<String>>();
	private static final Map<String, List<String>> MotsDe7Lettres = new HashMap<String, List<String>>();
	private static final Map<String, List<String>> MotsDe8Lettres = new HashMap<String, List<String>>();
	
//	private static int dico_loaded[7] = {0,0,0,0,0,0,0};

	private static int MotsDe2LettresLoaded = 0;
	private static int MotsDe3LettresLoaded = 0;
	private static int MotsDe4LettresLoaded = 0;
	private static int MotsDe5LettresLoaded = 0;
	private static int MotsDe6LettresLoaded = 0;
	private static int MotsDe7LettresLoaded = 0;
	private static int MotsDe8LettresLoaded = 0;
	
	private Mots() {
		
	}

	public static Map<String, List<String>> getMotsDeNLettres(int N)
	{
		switch (N)
		{
			case 2: return MotsDe2Lettres;
			case 3: return MotsDe3Lettres;
			case 4: return MotsDe4Lettres;
			case 5: return MotsDe5Lettres;
			case 6: return MotsDe6Lettres;
			case 7: return MotsDe7Lettres;
			case 8: return MotsDe8Lettres;
			default : return null;
		}	
	}

	public static int getMotsDeNLettresLoaded(int N)
	{
		switch (N)
		{
			case 2: return MotsDe2LettresLoaded;
			case 3: return MotsDe3LettresLoaded;
			case 4: return MotsDe4LettresLoaded;
			case 5: return MotsDe5LettresLoaded;
			case 6: return MotsDe6LettresLoaded;
			case 7: return MotsDe7LettresLoaded;
			case 8: return MotsDe8LettresLoaded;
			default : return 0;
		}	
		
	}
	/**
	 * Get Storage according to the number of letters
	 * @param nbLettres
	 * 		Number of letters
	 * @return
	 * 		Storage
	 */
	private static Map<String, List<String>> getStorage(int nbLettres) {
		switch (nbLettres) {
		case 2: MotsDe2LettresLoaded = 1;return MotsDe2Lettres;
		case 3: MotsDe3LettresLoaded = 1;return MotsDe3Lettres;
		case 4: MotsDe4LettresLoaded = 1;return MotsDe4Lettres;
		case 5: MotsDe5LettresLoaded = 1;return MotsDe5Lettres;
		case 6: MotsDe6LettresLoaded = 1;return MotsDe6Lettres;
		case 7: MotsDe7LettresLoaded = 1;return MotsDe7Lettres;
		case 8: MotsDe8LettresLoaded = 1;return MotsDe8Lettres;
		default: return null;
		}
	}

	/**
	 * Load a file 
	 * @param fileStream
	 * 		Input stream on the file
	 * @param nbLettres
	 * 		Letters number
	 * @throws IOException
	 */
	public static void loadFile(InputStream fileStream, int nbLettres) throws IOException {
		final Map<String, List<String>> storage = getStorage(nbLettres);
		BufferedReader br = new BufferedReader( new InputStreamReader(fileStream));
		String inString;
		try {
			while( (inString = br.readLine()) != null)
			{

				//break comma separated line using ","
				final String[] st = inString.split(",");
				List<String> liste_de_mots = new ArrayList<String>();
				String key=st[0];
				for (int i=1 ; i<st.length ; i++)
				{
					String token=st[i];
					liste_de_mots.add(token.toUpperCase());
				}
				//Log.d("solveur : ","key="+key);
				if (key!="")
				{
					if (key.equals("1"))
					{
						Chaine evaluate = new Chaine(liste_de_mots.get(0));
						key=String.valueOf(evaluate.getChaine_id());
					}
					//Log.d("solveur : ","key="+key+" ("+liste_de_mots+")");
					if (storage.containsKey(key))
					{
						// try to group all 
						storage.get(key).addAll(liste_de_mots);
						//Log.i("solveur : ","key="+key+" already exist ("+liste_de_mots.toString()+"\" / \""+storage.get(key).toString()+")");
					}
					else
					{
						storage.put(key, liste_de_mots);
					}
				}
			}
		}
		finally
		{
			if (br != null) {
				br.close();
			}
		}
	}  

}
